/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.exec;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractSetOfEntities {

	private static String tagEntry = "(?:\\(([^:]+):([^:]+)\\))";
	private static String tagHead = "(?:(?:\\S+->\\S+.*)|(?:[^\t]+))";

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int part = Integer.parseInt(args[0]);
		String s;
		String buffer  = "";
		String id = "";
		while ((s = in.readLine()) != null)
		{
			if (s.isEmpty())
			{
				System.out.println(id + "\t" + setToString(getEntries(buffer, part)));
				buffer  = "";
				id = "";
			}
			else{
				if (s.matches(tagHead))
				{
					id = getID(s);
				}
				else{
					buffer += s + "\n";
				}
			}
		}
	}


	private static String getID(String s) {
		 return s.split("\\s+")[0];	
	}


	private static String setToString (Set<String> set)
	{
		return set.toString().replaceAll("[\\[,\\]]", "");
	}

	private static Set<String> getEntries (String s, int partNumber)
	{
		Set<String> result = new HashSet<String>();
		Pattern reco = Pattern.compile(tagEntry);
		Matcher m;
		String[] tab = s.split("\n");
		for (int i = 0; i< tab.length;i++)
		{
			m = reco.matcher(tab[i]);
			while (m.find())
			{
				result.add(m.group(partNumber));
			}
		}
		return result;
	}
}